package com.wcy.filter;

import com.wcy.util.JwtTokenUtil;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.io.PrintWriter;

@Component
@Order(2)
public class GatewayAuthFilter extends OncePerRequestFilter {

    private JwtTokenUtil util;

    @Value("${security.internal.token}")
    private String internalGatewayToken;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        System.out.println("from:"+request.getRequestURI());

        String gatewayToken = request.getHeader("X-My-Gateway-Token");
//        System.out.println("uri:"+request.getRequestURI());
        if(gatewayToken != null && gatewayToken.equals(internalGatewayToken)){
            filterChain.doFilter(request, response);
            return;
        }
        response.setStatus(403);
    }
}
